package com.itheima.aop;

import com.itheima.mapper.OperateLogMapper;
import com.itheima.pojo.OperateLog;
import com.itheima.utils.CurrentHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @author hu
 * @date 2025/3/12
 */
@Slf4j
@Component
@Aspect
public class OperationLogAspect {

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Around("@annotation(com.itheima.anno.OperationLog)")
    public Object OperationLog(ProceedingJoinPoint pjp) throws Throwable {

        long begin = System.currentTimeMillis();
        Object result = pjp.proceed();
        long end = System.currentTimeMillis();

        OperateLog logs = new OperateLog();
        //从线程中获取员工ID
        Integer empId = CurrentHolder.getCurrentId();

        logs.setOperateEmpId(empId);
        logs.setOperateTime(LocalDateTime.now());
        logs.setClassName(pjp.getTarget().getClass().getName());
        logs.setMethodParams(Arrays.toString(pjp.getArgs()));
        logs.setMethodName(pjp.getSignature().getName());
        logs.setReturnValue(result != null ? result.toString() : "void");
        logs.setCostTime(end - begin);
        //保持日志
        log.info("记录操作日志{}",logs);
        operateLogMapper.insert(logs);
        return result;
    }
}




  /*  @Around("execution(* com.itheima.controller.EmpController.updateEmp(..))||" +
              "execution(* com.itheima.controller.EmpController.save(..))||" +
              "execution(* com.itheima.controller.EmpController.deleteEmp(..))")*/